package com.eascs.admin.o2o.loan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkpInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBksyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanBkxyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsMainproductInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScToptencInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScToptensInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanBkService;
import com.eascs.admin.o2o.loan.service.O2oLoanBkpService;
import com.eascs.admin.o2o.loan.service.O2oLoanBksyService;
import com.eascs.admin.o2o.loan.service.O2oLoanBkxyService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsMainProductService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsService;
import com.eascs.admin.o2o.loan.service.O2oLoanScService;
import com.eascs.admin.o2o.loan.service.O2oLoanScTopTenCService;
import com.eascs.admin.o2o.loan.service.O2oLoanScTopTenSService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@RestController
@RequestMapping("/rmongo")
public class O2oLonaScGsRestController extends BaseRestController {

	@Autowired
	O2oLoanGsService gsService;
	@Autowired
	O2oLoanGsMainProductService productService;
	@Autowired
	O2oLoanScService scService;
	@Autowired
	O2oLoanScTopTenCService cService;
	@Autowired
	O2oLoanScTopTenSService tenSService;

	@Autowired
	O2oLoanBkpService o2oLoanBkpService;
	@Autowired
	O2oLoanBkService o2oLoanBkService;
	@Autowired
	O2oLoanBkxyService o2oLoanBkxyService;
	@Autowired
	O2oLoanBksyService o2oLoanBksyService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	@Value("${o2o.admin.crm}")
	String crmUrl;

	/**
	 * @Description:
	 * 				<p>
	 *               主要产品销售情况列表
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:15
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_plist")
	public BaseRestResult<String> viewLoanGsInfo(String loanapplyNo, String type) {
		O2oLoanGsMainproductInfo gsInfo = new O2oLoanGsMainproductInfo();
		gsInfo.setLoanapplyNo(loanapplyNo);
		Button[] btn = null;
		if (!type.equals("VIEW")) {
			Button editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/gs_info_editp?type=addp&id=",
					"UpdateInternalInfo");
			Button deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/gs_info_deletep?type=addp&id=",
					"UpdateInternalInfo");
			btn = new Button[] { editeBtn, deleteBtn };
		}
		String[] titles = { "产品名称", "产地（仅水果）", "上月销量" };
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("PLACE_OF_ORIGIN"), null };
		PaginationInfo paginationInfo = new PaginationInfo(productService.queryPage(gsInfo, 10, 0), titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(btn);
		ViewContext.put("paginationInfo", paginationInfo);
		return new HtmlRestResult("loanManagement/sg_listinfo");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               蔬果贷【经营补充信息】
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:40
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_lno")
	public BaseRestResult<String> loanGsInfo(String loanapplyNo, String type) {
		O2oLoanGsInfo gsInfo = gsService.getLoanapplyNo(loanapplyNo);
		String url = "loanManagement/loan_gs_info_view";
		if (!type.equals("VIEW")) {
			url = "loanManagement/loan_gs_info";
		}
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("entity", gsInfo);
		ViewContext.put("crmUrl", crmUrl);
		return new HtmlRestResult(url);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               编辑和添加主要产品销售情况
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:56
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_addp")
	public BaseRestResult<String> addGsInfo(String loanapplyNo, String type) {
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("edit", "");
		String url = "";
		if (type.equals("addp")) {//蔬果贷新增主要产品销售情况
			url = "loanManagement/gs_add_info";
		} else if (type.equals("addc")) {//商超贷新增十大下游客户近6月供货量
			url = "loanManagement/sc_add_info";
		} else//商超贷新增十大销售产品
			url = "loanManagement/sg_add_info";
		return HtmlRestResult.popHtmlFragment(url);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               编辑和添加主要产品销售情况
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:56
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_editp")
	public BaseRestResult<String> gs_info_editp(String id, String type) {
		ViewContext.put("edit", "edit");
		String url = "";
		if (type.equals("addp")) {
			O2oLoanGsMainproductInfo gsInfo = productService.getEntityById(id);
			ViewContext.put("loanapplyNo", gsInfo.getLoanapplyNo());
			ViewContext.put("entity", gsInfo);
			url = "loanManagement/gs_add_info";
		} else if (type.equals("addc")) {
			O2oLoanScToptencInfo gsInfo = cService.getEntityById(id);
			ViewContext.put("loanapplyNo", gsInfo.getLoanapplyNo());
			ViewContext.put("entity", gsInfo);
			url = "loanManagement/sc_add_info";
		} else {
			O2oLoanScToptensInfo gsInfo = tenSService.getEntityById(id);
			ViewContext.put("loanapplyNo", gsInfo.getLoanapplyNo());
			ViewContext.put("entity", gsInfo);
			url = "loanManagement/sg_add_info";
		}
		return HtmlRestResult.popHtmlFragment(url);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               添加保存主要产品信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:41:39
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_addpsave")
	public BaseRestResult<String> addSaveGsInfo(O2oLoanGsMainproductInfo t) {
		if (StringUtil.isBlank(t.getProductName())) {
			return BaseRestResult.warn("产品名称不能为空");
		}
		if (StringUtil.isBlank(t.getProductionAddress())) {
			return BaseRestResult.warn("产地不能为空");
		}
		if (StringUtil.isBlank(t.getLastMonthSaleNum())) {
			return BaseRestResult.warn("上月销售量不能为空");
		}
		int i = productService.insertEntity(t);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               编辑保存主要产品信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:42:26
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_editpsave")
	public BaseRestResult<String> editGsInfo(O2oLoanGsMainproductInfo t) {
		if (StringUtil.isBlank(t.getProductName())) {
			return BaseRestResult.warn("产品名称不能为空");
		}
		if (StringUtil.isBlank(t.getProductionAddress())) {
			return BaseRestResult.warn("产地不能为空");
		}
		if (StringUtil.isBlank(t.getLastMonthSaleNum())) {
			return BaseRestResult.warn("上月销售量不能为空");
		}
		int i = productService.updateEntity(t);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               蔬果贷【经营补充信息】保存表单
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:43:00
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_editsave")
	public BaseRestResult<String> editSaveGsInfo(O2oLoanGsInfo t) {
		O2oLoanGsInfo gsInfo = gsService.getLoanapplyNo(t.getLoanapplyNo());
		if (null == t.getStartOperateTime()) {
			return BaseRestResult.warn("本市场经营开始时间不能为空");
		}
		if (StringUtil.isBlank(t.getIsParking())) {
			return BaseRestResult.warn("档口有无车位不能为空");
		}
		if (StringUtil.isBlank(t.getIsOwnFarm())) {
			return BaseRestResult.warn("是否有自有农场不能为空");
		}
		if (StringUtil.isBlank(t.getIsFarmer())) {
			return BaseRestResult.warn("是否向固定农户采购不能为空");
		}
		if (StringUtil.isBlank(t.getIsOtherPurchase())) {
			return BaseRestResult.warn("是否向其他供菜商采购不能为空");
		}
		// if (StringUtil.isBlank(t.getOtherMode())) {
		// return BaseRestResult.warn("其他方式不能为空");
		// }
		if (StringUtil.isBlank(t.getTwoBatch())) {
			return BaseRestResult.warn("二批商不能为空");
		}
		if (StringUtil.isBlank(t.getSupermarket())) {
			return BaseRestResult.warn("商超不能为空");
		}
		if (StringUtil.isBlank(t.getGroupPurch())) {
			return BaseRestResult.warn("团购不能为空");
		}
		if (StringUtil.isBlank(t.getRetail())) {
			return BaseRestResult.warn("零售终端不能为空");
		}
		int i = 0;
		if (null != gsInfo) {
			gsInfo.setStartOperateTime(t.getStartOperateTime());
			gsInfo.setIsParking(t.getIsParking());
			gsInfo.setIsOwnFarm(t.getIsOwnFarm());
			gsInfo.setIsFarmer(t.getIsFarmer());
			gsInfo.setIsOtherPurchase(t.getIsOtherPurchase());
			gsInfo.setOtherMode(t.getOtherMode());
			gsInfo.setTwoBatch(t.getTwoBatch());
			gsInfo.setSupermarket(t.getSupermarket());
			gsInfo.setGroupPurch(t.getGroupPurch());
			gsInfo.setRetail(t.getRetail());
			gsInfo.setOfficeprovince(t.getOfficeprovince());
			gsInfo.setOfficecity(t.getOfficecity());
			gsInfo.setOfficecounty(t.getOfficecounty());
			gsInfo.setDetailAddress(t.getDetailAddress());
			gsInfo.setOtherModeRemark(t.getOtherModeRemark());
			i = gsService.updateEntity(gsInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else
			i = gsService.insertEntity(t);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	@RequestMapping("/sc_info_lno")
	public BaseRestResult<String> loanScInfo(String loanapplyNo, String type) {
		O2oLoanScInfo gsInfo = scService.getLoanapplyNo(loanapplyNo);
		String url = "loanManagement/loan_sc_info_view";
		if (!type.equals("VIEW")) {
			url = "loanManagement/loan_sc_info";
		}
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("entity", gsInfo);
		return new HtmlRestResult(url);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               十大下游客户近6月供货量
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:15
	 * @version V1.0
	 */
	@RequestMapping("/sc_info_clist")
	public BaseRestResult<String> viewLoanScInfo(String loanapplyNo, String type) {
		O2oLoanScToptencInfo gsInfo = new O2oLoanScToptencInfo();
		gsInfo.setLoanapplyNo(loanapplyNo);
		Button[] btn = null;
		if (!type.equals("VIEW")) {
			Button editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/gs_info_editp?type=addc&id=",
					"UpdateInternalInfo");
			Button deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/gs_info_deletep?type=addc&id=",
					"UpdateInternalInfo");
			btn = new Button[] { editeBtn, deleteBtn };
		}
		String[] titles = { "客户名称", "结算账期", "供货量" };
		PaginationInfo paginationInfo = new PaginationInfo(cService.queryPage(gsInfo, 10, 0), titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setMoreButtons(btn);
		ViewContext.put("paginationInfo", paginationInfo);
		return new HtmlRestResult("loanManagement/sg_listinfo");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               十大销售产品
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:40:15
	 * @version V1.0
	 */
	@RequestMapping("/sc_info_glist")
	public BaseRestResult<String> viewLoanScgInfo(String loanapplyNo, String type) {
		O2oLoanScToptensInfo gsInfo = new O2oLoanScToptensInfo();
		gsInfo.setLoanapplyNo(loanapplyNo);
		Button[] btn = null;
		if (!type.equals("VIEW")) {
			Button editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/gs_info_editp?type=addg&id=",
					"UpdateInternalInfo");
			Button deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/gs_info_deletep?type=addg&id=",
					"UpdateInternalInfo");
			btn = new Button[] { editeBtn, deleteBtn };
		}
		String[] titles = { "产品品牌", "产品类型", "代理资质", "合作期限", "年销售任务（万元）" };
		PaginationInfo paginationInfo = new PaginationInfo(tenSService.queryPage(gsInfo, 10, 0), titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setMoreButtons(btn);
		ViewContext.put("paginationInfo", paginationInfo);
		return new HtmlRestResult("loanManagement/sg_listinfo");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               删除通用方法
	 *               </p>
	 * @author CXW
	 * @date 2017年10月19日 下午2:55:31
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_deletep")
	public BaseRestResult<String> deleteLoanScgInfo(String id, String type) {
		int i = 0;
		if (type.equals("addp")) {
			i = productService.delete(id);
		} else if (type.equals("addc")) {
			i = cService.delete(id);
		} else {
			i = tenSService.delete(id);
		}
		if (i > 0) {
			return BaseRestResult.info("删除成功").andRefresh();
		} else {
			return BaseRestResult.warn("删除失败");
		}
	}

	/**
	 * @Description:
	 * 				<p>
	 *               添加保存十大下游客户近6月供货量信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:41:39
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_addcsave")
	public BaseRestResult<String> addSavecInfo(O2oLoanScToptencInfo t) {
		// if (StringUtil.isBlank(t.getCusName())) {
		// return BaseRestResult.warn("客户名称不能为空");
		// }
		// if (null == t.getSettlementDate()) {
		// return BaseRestResult.warn("结算账期不能为空");
		// }
		// if (StringUtil.isBlank(t.getSupplyQuantity())) {
		// return BaseRestResult.warn("供货量不能为空");
		// }
		int i = cService.insertEntity(t);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               编辑保存十大下游客户近6月供货量信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:42:26
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_editcsave")
	public BaseRestResult<String> editCInfo(O2oLoanScToptencInfo t) {
		// if (StringUtil.isBlank(t.getCusName())) {
		// return BaseRestResult.warn("客户名称不能为空");
		// }
		// if (null == t.getSettlementDate()) {
		// return BaseRestResult.warn("结算账期不能为空");
		// }
		// if (StringUtil.isBlank(t.getSupplyQuantity())) {
		// return BaseRestResult.warn("供货量不能为空");
		// }
		O2oLoanScToptencInfo info = cService.getEntityById(t.getId());
		info.setCusName(t.getCusName());
		info.setSettlementDate(t.getSettlementDate());
		info.setSupplyQuantity(t.getSupplyQuantity());
		int i = cService.updateEntity(info);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               添加保存十大销售产品信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:41:39
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_addgsave")
	public BaseRestResult<String> addSctengInfo(O2oLoanScToptensInfo t) {
		// if (StringUtil.isBlank(t.getProductBrand())) {
		// return BaseRestResult.warn("产品品牌不能为空");
		// }
		// if (StringUtil.isBlank(t.getProductType())) {
		// return BaseRestResult.warn("产品类型不能为空");
		// }
		// if (StringUtil.isBlank(t.getAgentQuali())) {
		// return BaseRestResult.warn("代理资质不能为空");
		// }
		// if (StringUtil.isBlank(t.getCoopTerm())) {
		// return BaseRestResult.warn("合作年限不能为空");
		// }
		// if (StringUtil.isBlank(t.getY_saleTask())) {
		// return BaseRestResult.warn("年销售任务不能为空");
		// }
		tenSService.insertEntity(t);
		return BaseRestResult.info("保存成功").andRefresh();
	}

	/**
	 * @Description:
	 * 				<p>
	 *               编辑保存十大销售产品信息
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:42:26
	 * @version V1.0
	 */
	@RequestMapping("/gs_info_editgsave")
	public BaseRestResult<String> editSctengInfo(O2oLoanScToptensInfo t) {
		// if (StringUtil.isBlank(t.getProductBrand())) {
		// return BaseRestResult.warn("产品品牌不能为空");
		// }
		// if (StringUtil.isBlank(t.getProductType())) {
		// return BaseRestResult.warn("产品类型不能为空");
		// }
		// if (StringUtil.isBlank(t.getAgentQuali())) {
		// return BaseRestResult.warn("代理资质不能为空");
		// }
		// if (StringUtil.isBlank(t.getCoopTerm())) {
		// return BaseRestResult.warn("合作年限不能为空");
		// }
		// if (StringUtil.isBlank(t.getY_saleTask())) {
		// return BaseRestResult.warn("年销售任务不能为空");
		// }
		O2oLoanScToptensInfo info = tenSService.getEntityById(t.getId());
		info.setProductBrand(t.getProductBrand());
		info.setProductType(t.getProductType());
		info.setAgentQuali(t.getAgentQuali());
		info.setCoopTerm(t.getCoopTerm());
		info.setY_saleTask(t.getY_saleTask());
		tenSService.updateEntity(info);
		return BaseRestResult.info("保存成功").andRefresh();
	}

	/**
	 * @Description:
	 * 				<p>
	 *               商超贷【经营补充信息】保存表单
	 *               </p>
	 * @author CXW
	 * @date 2017年10月16日 下午7:43:00
	 * @version V1.0
	 */
	@RequestMapping("/sc_info_editsave")
	public BaseRestResult<String> editSaveScInfo(O2oLoanScInfo t) {
		O2oLoanScInfo gsInfo = scService.getLoanapplyNo(t.getLoanapplyNo());
		if (null == t.getWhitelistTime()) {
			return BaseRestResult.warn("与白名单内商超合作开始时间不能为空");
		}
		if (StringUtil.isBlank(t.getTwoBatch())) {
			return BaseRestResult.warn("二批商不能为空");
		}
		if (StringUtil.isBlank(t.getSupermarket())) {
			return BaseRestResult.warn("商超不能为空");
		}
		if (StringUtil.isBlank(t.getDirectRetail())) {
			return BaseRestResult.warn("直接零售不能为空");
		}
		if (StringUtil.isBlank(t.getRetail())) {
			return BaseRestResult.warn("零售终端不能为空");
		}
		int i = 0;
		if (null != gsInfo) {
			gsInfo.setWhitelistTime(t.getWhitelistTime());
			gsInfo.setTwoBatch(t.getTwoBatch());
			gsInfo.setSupermarket(t.getSupermarket());
			gsInfo.setDirectRetail(t.getDirectRetail());
			gsInfo.setRetail(t.getRetail());
			i = scService.updateEntity(gsInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else
			i = scService.insertEntity(t);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @describe 显示经营补充信息
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bk_info_view")
	public BaseRestResult<String> bk_info_view(String loanapplyNo, String type) {
		O2oLoanBkInfo bkInfo = o2oLoanBkService.getEntity(loanapplyNo);
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		String url = "loanManagement/loan_bk_info_view";
		if (!type.equals("VIEW")) {
			url = "loanManagement/loan_bk_info";
		}
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("entity", bkInfo);
		return new HtmlRestResult(url);
	}

	/**
	 * @describe 显示添加页面保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bk_info_add")
	public BaseRestResult<String> bk_info_add(String loanapplyNo, String type) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("loanapply", loanapply);
		String url = "";
		if (type.equals("addbk")) {//终端贷编辑渠道数据
			O2oLoanBkInfo loanBkpInfo = o2oLoanBkService.getEntity(loanapplyNo);
			ViewContext.put("loanapplyNo", loanapplyNo);
			ViewContext.put("entity", loanBkpInfo);
			url = "loanManagement/bk_edit_info";
		} else if (type.equals("addsy")) {//终端贷新增上游信息
			url = "loanManagement/bksy_add_info";
		} else if (type.equals("addxy")) {//终端贷新增下游信息
			url = "loanManagement/bkxy_add_info";
		} else if (type.equals("addbkp")) {//终端贷新增 主要销售产品
			url = "loanManagement/bkp_add_info";
		}
		return HtmlRestResult.popHtmlFragment(url);
	}

	/**
	 * @describe 显示编辑页面保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bk_info_edit")
	public BaseRestResult<String> bk_info_edit(String id, String type) {
		String url = "";
		if (type.equals("addbksy")) {
			O2oLoanBksyInfo loanBksyInfo = o2oLoanBksyService.getEntityById(id);
			ViewContext.put("loanapplyNo", loanBksyInfo.getLoanapplyId());
			ViewContext.put("entity", loanBksyInfo);
			url = "loanManagement/bksy_add_info";
		} else if (type.equals("addbkxy")) {
			O2oLoanBkxyInfo loanBkxyInfo = o2oLoanBkxyService.getEntityById(id);
			ViewContext.put("loanapplyNo", loanBkxyInfo.getLoanapplyId());
			ViewContext.put("entity", loanBkxyInfo);
			url = "loanManagement/bkxy_add_info";
		} else if (type.equals("addbkp")) {
			O2oLoanBkpInfo loanBkpInfo = o2oLoanBkpService.getEntityById(id);
			ViewContext.put("loanapplyNo", loanBkpInfo.getLoanapplyId());
			ViewContext.put("entity", loanBkpInfo);
			url = "loanManagement/bkp_add_info";
		}
		return HtmlRestResult.popHtmlFragment(url);
	}

	/**
	 * @describe 终端贷渠道保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bk_add_save")
	public BaseRestResult<String> bk_add_save(O2oLoanBkInfo loanBkInfo) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanBkInfo.getLoanapplyId());
		if (StringUtil.isBlank(loanBkInfo.getProductName())) {
			return BaseRestResult.warn("与渠道企业采购的产品不能为空");
		}
		if (null == loanBkInfo.getMonthPrice()) {
			return BaseRestResult.warn("月均合作额不能为空");
		}
		//对终端贷、流通贷——厂家、流通贷——经销商三种类型增加内审表单字段"与渠道企业合作起始时间"
		if ("BP".equals(loanapply.getLoanType()) || "BP_JX".equals(loanapply.getLoanType()) || "BK".equals(loanapply.getLoanType())) {
			if (null == loanBkInfo.getStartCoopDate()) {
				return BaseRestResult.warn("与渠道企业合作起始时间不能为空");
			}
		}
		if (StringUtil.isBlank(loanBkInfo.getId())) {
			o2oLoanBkService.insertEntity(loanBkInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else {
			O2oLoanBkInfo bkInfo = o2oLoanBkService.getEntityById(loanBkInfo.getId());
			bkInfo.setLoanapplyId(loanBkInfo.getLoanapplyId());
			bkInfo.setMonthPrice(loanBkInfo.getMonthPrice());
			bkInfo.setProductName(loanBkInfo.getProductName());
			bkInfo.setStartCoopDate(loanBkInfo.getStartCoopDate());
			o2oLoanBkService.updateEntity(bkInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		}
	}

	/**
	 * @describe 终端贷渠道主要产品保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bkp_add_save")
	public BaseRestResult<String> bkp_add_save(O2oLoanBkpInfo loanBkpInfo) {
		if (StringUtil.isBlank(loanBkpInfo.getProductName())) {
			return BaseRestResult.warn("产品名称不能为空");
		}
		if (StringUtil.isBlank(loanBkpInfo.getBrand())) {
			return BaseRestResult.warn("品牌不能为空");
		}
		if (StringUtil.isBlank(loanBkpInfo.getModel())) {
			return BaseRestResult.warn("型号不能为空");
		}
		if (null == loanBkpInfo.getBuyingPrice()) {
			return BaseRestResult.warn("进货价不能为空");
		}
		if (null == loanBkpInfo.getSellingPrice()) {
			return BaseRestResult.warn("销售价不能为空");
		}
		if (null == loanBkpInfo.getLastMonthSales()) {
			return BaseRestResult.warn("上月销量不能为空");
		}
		if (StringUtil.isBlank(loanBkpInfo.getId())) {
			loanBkpInfo.setIsDelete(StatusContexts.O2O_NO);
			o2oLoanBkpService.insertEntity(loanBkpInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else {
			O2oLoanBkpInfo bkpInfo = o2oLoanBkpService.getEntityById(loanBkpInfo.getId());
			bkpInfo.setBrand(loanBkpInfo.getBrand());
			bkpInfo.setBuyingPrice(loanBkpInfo.getBuyingPrice());
			bkpInfo.setLastMonthSales(loanBkpInfo.getLastMonthSales());
			bkpInfo.setLoanapplyId(loanBkpInfo.getLoanapplyId());
			bkpInfo.setModel(loanBkpInfo.getModel());
			bkpInfo.setProductName(loanBkpInfo.getProductName());
			bkpInfo.setSellingPrice(loanBkpInfo.getSellingPrice());
			o2oLoanBkpService.updateEntity(bkpInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		}
	}

	/**
	 * @describe 终端贷渠道下游保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bkxy_add_save")
	public BaseRestResult<String> bkxy_add_save(O2oLoanBkxyInfo loanBkxyInfo) {
		if (StringUtil.isBlank(loanBkxyInfo.getCustomerName())) {
			return BaseRestResult.warn("下游客户不能为空");
		}
		if (null == loanBkxyInfo.getSales()) {
			return BaseRestResult.warn("销售比例不能为空");
		}
		if (StringUtil.isBlank(loanBkxyInfo.getSettlement())) {
			return BaseRestResult.warn("结算方式不能为空");
		}
		if (StringUtil.isBlank(loanBkxyInfo.getPayment())) {
			return BaseRestResult.warn("回款情况不能为空");
		}
		if (StringUtil.isBlank(loanBkxyInfo.getId())) {
			loanBkxyInfo.setIsDelete(StatusContexts.O2O_NO);
			o2oLoanBkxyService.insertEntity(loanBkxyInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else {
			O2oLoanBkxyInfo bkxyInfo = o2oLoanBkxyService.getEntityById(loanBkxyInfo.getId());
			bkxyInfo.setCustomerName(loanBkxyInfo.getCustomerName());
			bkxyInfo.setLoanapplyId(loanBkxyInfo.getLoanapplyId());
			bkxyInfo.setPayment(loanBkxyInfo.getPayment());
			bkxyInfo.setSales(loanBkxyInfo.getSales());
			bkxyInfo.setSettlement(loanBkxyInfo.getSettlement());
			o2oLoanBkxyService.updateEntity(bkxyInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		}
	}

	/**
	 * @describe 终端贷渠道上游保存
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bksy_add_save")
	public BaseRestResult<String> bksy_add_save(O2oLoanBksyInfo loanBksyInfo) {
		if (StringUtil.isBlank(loanBksyInfo.getSupplier())) {
			return BaseRestResult.warn("供应商不能为空");
		}
		if (null == loanBksyInfo.getPurchase()) {
			return BaseRestResult.warn("采购占比不能为空");
		}
		if (StringUtil.isBlank(loanBksyInfo.getCooperation())) {
			return BaseRestResult.warn("合作产品不能为空");
		}
		if (StringUtil.isBlank(loanBksyInfo.getPeriod())) {
			return BaseRestResult.warn("账期不能为空");
		}
		if (StringUtil.isBlank(loanBksyInfo.getId())) {
			loanBksyInfo.setIsDelete(StatusContexts.O2O_NO);
			o2oLoanBksyService.insertEntity(loanBksyInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		} else {
			O2oLoanBksyInfo bksyInfo = o2oLoanBksyService.getEntityById(loanBksyInfo.getId());
			bksyInfo.setCooperation(loanBksyInfo.getCooperation());
			bksyInfo.setLoanapplyId(loanBksyInfo.getLoanapplyId());
			bksyInfo.setPeriod(loanBksyInfo.getPeriod());
			bksyInfo.setPurchase(loanBksyInfo.getPurchase());
			bksyInfo.setSupplier(loanBksyInfo.getSupplier());
			o2oLoanBksyService.updateEntity(loanBksyInfo);
			return BaseRestResult.info("保存成功").andRefresh();
		}
	}

	/**
	 * @describe 删除
	 * @author CXW
	 * @param loanapplyNo
	 * @param type
	 * @return
	 */
	@RequestMapping("/bk_info_delete")
	public BaseRestResult<String> bk_info_delete(String id, String type) {
		if (type.endsWith("addbkp")) {
			O2oLoanBkpInfo loanBkpInfo = o2oLoanBkpService.getEntityById(id);
			loanBkpInfo.setIsDelete(StatusContexts.O2O_YES);
			o2oLoanBkpService.updateEntity(loanBkpInfo);
		} else if (type.endsWith("addbkxy")) {
			O2oLoanBkxyInfo loanBkxyInfo = o2oLoanBkxyService.getEntityById(id);
			loanBkxyInfo.setIsDelete(StatusContexts.O2O_YES);
			o2oLoanBkxyService.updateEntity(loanBkxyInfo);
		} else if (type.endsWith("addbksy")) {
			O2oLoanBksyInfo loanBksyInfo = o2oLoanBksyService.getEntityById(id);
			loanBksyInfo.setIsDelete(StatusContexts.O2O_YES);
			o2oLoanBksyService.updateEntity(loanBksyInfo);
		}
		return BaseRestResult.info("删除成功").andRefresh();
	}

	/**
	 * 列表显示
	 * 
	 * @param loanapplyNo
	 * @param type
	 *            显示或编辑
	 * @param loanInfoType
	 *            上游、下游、销售产品
	 * @return
	 */
	@RequestMapping("/bk_info_list")
	public BaseRestResult<String> viewLoanBksyInfo(String loanapplyNo, String type, String loanInfoType) {
		O2oLoanBkpInfo loanBkpInfo = new O2oLoanBkpInfo();
		loanBkpInfo.setLoanapplyId(loanapplyNo);
		O2oLoanBkxyInfo loanBkxyInfo = new O2oLoanBkxyInfo();
		loanBkxyInfo.setLoanapplyId(loanapplyNo);
		O2oLoanBksyInfo loanBksyInfo = new O2oLoanBksyInfo();
		loanBksyInfo.setLoanapplyId(loanapplyNo);
		Button[] btn = null;
		String[] titles = null;
		PaginationInfo paginationInfo = null;
		if (!type.equals("VIEW")) {
			Button editeBtn = null;
			Button deleteBtn = null;
			if (loanInfoType.endsWith("bkp")) {
				editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/bk_info_edit?type=addbkp&id=",
						"UpdateInternalInfo");
				deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/bk_info_delete?type=addbkp&id=",
						"UpdateInternalInfo");
			} else if (loanInfoType.endsWith("bkxy")) {
				editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/bk_info_edit?type=addbkxy&id=",
						"UpdateInternalInfo");
				deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/bk_info_delete?type=addbkxy&id=",
						"UpdateInternalInfo");
			} else if (loanInfoType.endsWith("bksy")) {
				editeBtn = ButtonHelper.createAjaxButton("编辑", "/rmongo/bk_info_edit?type=addbksy&id=",
						"UpdateInternalInfo");
				deleteBtn = ButtonHelper.createAjaxButton("删除", "/rmongo/bk_info_delete?type=addbksy&id=",
						"UpdateInternalInfo");
			}
			btn = new Button[] { editeBtn, deleteBtn };
		}
		if (loanInfoType.endsWith("bkp")) {//主要销售产品
			titles = new String[] { "产品名称", "品牌", "型号", "进货价", "销售价", "上月销量" };
			paginationInfo = new PaginationInfo(o2oLoanBkpService.queryPage(loanBkpInfo, 10, 0), titles);
		} else if (loanInfoType.endsWith("bkxy")) {//下游
			titles = new String[] { "下游客户", "销售比例（%）", "结算方式", "回款情况" };
			paginationInfo = new PaginationInfo(o2oLoanBkxyService.queryPage(loanBkxyInfo, 10, 0), titles);
		} else if (loanInfoType.endsWith("bksy")) {// 上游
			titles = new String[] { "供应商", "采购占比（%）", "合作产品", "账期" };
			paginationInfo = new PaginationInfo(o2oLoanBksyService.queryPage(loanBksyInfo, 10, 0), titles);
		}
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setMoreButtons(btn);
		ViewContext.put("paginationInfo", paginationInfo);
		return new HtmlRestResult("loanManagement/sg_listinfo");
	}

}
